import axios from "axios";

import {ref} from "vue";

// 定义总数据量的类型
interface ChartData {
    id: number,
    goodsId: number,
    goodsName: string,
    cost: number,
    wareHouseId: string,
    type: string,
    customerName: string,
    customerPhone: string,
    dealCount: number,
    dealTime: string,
    dealPurpose: string,
    goodsQuality: number,
    userId: number
    userName: string,
}

// 定义词云图的数据结构
interface CloudChartData {
    name: string; // 货物名称
    value: number; // 出现次数
    dealTime: string,//交易时间
}

// 定义饼图的数据结构
interface PanChartData {
    name: string; // 交易类型
    dealTime: string,//交易时间
}

// 定义金额树形图的数据结构
interface MoneyTreeChartData {
    name: string; // 货物名称
    outValue: number; // 售出金额
    inValue: number; // 购入金额
    dealTime: string,//交易时间
}

// 定义次数树形图的数据结构
interface CountTreeChartData {
    name: string; // 货物名称
    outValue: number; // 售出总次数
    inValue: number; // 购入总次数
    dealTime: string,//交易时间
}

//  定义地区热力图
type AreaHeatMapData = {
    type: string;//  交易类型
    area: string; // 交易区域
    dealTime: string,//交易时间
}

//  定义员工业绩折线图
interface EmployeePerformanceChartData {
    name: string;//  员工username
    value: number;//  负责订单的数量*0.2+订单金额*0.8
    dealTime: string,//交易时间
}

//  定义员工评分雷达图
interface EmployeeChartData {
    name: string;// 员工username
    count: number;//售出金额*0.7+购入金额*0.3
    quality: number;// 负责订单的货物质量
    dealCount: number;// 负责订单的数量
    dealTime: string,//交易时间
}

const chartData = ref<ChartData[]>()
const total = ref(0)
const outCloudChartData = ref<CloudChartData[]>([])
const inCloudChartData = ref<CloudChartData[]>([])
const transferCloudChartData = ref<CloudChartData[]>([])
const panChartData = ref<PanChartData[]>([])
const moneyTreeChartData = ref<MoneyTreeChartData[]>([])
const countTreeChartData = ref<CountTreeChartData[]>([])
const areaHeatMapData = ref<AreaHeatMapData[]>([])
const employeePerformanceChartData = ref<EmployeePerformanceChartData[]>([])
const employeeChartData = ref<EmployeeChartData[]>([])


const tempOutCloudChartData = ref<CloudChartData>(null)
const tempInCloudChartData = ref<CloudChartData>(null)
const tempTransferCloudChartData = ref<CloudChartData>(null)
const tempPanChartData = ref<PanChartData>(null)
const tempMoneyTreeChartData = ref<MoneyTreeChartData>(null)
const tempCountTreeChartData = ref<CountTreeChartData>(null)
const tempAreaHeatMapData = ref<AreaHeatMapData>(null)
const tempEmployeePerformanceChartData = ref<EmployeePerformanceChartData>(null)
const tempEmployeeChartData = ref<EmployeeChartData>(null)


function getData() {
    axios.get("http://localhost:8080/order/findAll").then(res => {
        //所有图标相关数据初始化为空
        chartData.value = null;
        total.value = null;
        outCloudChartData.value = [];
        inCloudChartData.value = [];
        transferCloudChartData.value = [];
        panChartData.value = [];
        moneyTreeChartData.value = [];
        countTreeChartData.value = [];
        areaHeatMapData.value = []
        employeePerformanceChartData.value = []
        employeeChartData.value = []

        chartData.value = res.data.data
        total.value = res.data.total
        console.log("总交易=============================")
        console.log(chartData)
        for (let Data in chartData.value) {

            //  出售
            if (chartData.value[Data].type === "售出") {
                // 出售词云
                tempOutCloudChartData.value = {
                    name: chartData.value[Data].goodsName,
                    value: chartData.value[Data].dealCount,
                    dealTime: chartData.value[Data].dealTime
                }
                outCloudChartData.value.push(tempOutCloudChartData.value)

                //金额树形图
                tempMoneyTreeChartData.value = {
                    name: chartData.value[Data].goodsName,
                    outValue: chartData.value[Data].cost,
                    inValue: 0,
                    dealTime: chartData.value[Data].dealTime
                }
                moneyTreeChartData.value.push(tempMoneyTreeChartData.value)

                // 售出/购入量树形图
                tempCountTreeChartData.value = {
                    name: chartData.value[Data].goodsName,
                    outValue: chartData.value[Data].dealCount,
                    inValue: 0,
                    dealTime: chartData.value[Data].dealTime
                }
                countTreeChartData.value.push(tempCountTreeChartData.value)

                //地区热力图
                tempAreaHeatMapData.value = {
                    type: chartData.value[Data].type,
                    area: chartData.value[Data].wareHouseId,
                    dealTime: chartData.value[Data].dealTime
                }
                areaHeatMapData.value.push(tempAreaHeatMapData.value)

                //员工评分雷达图
                tempEmployeeChartData.value = {
                    name: chartData.value[Data].userName,
                    count: chartData.value[Data].cost * 0.7,
                    quality: chartData.value[Data].goodsQuality,
                    dealCount: 1,
                    dealTime: chartData.value[Data].dealTime
                }
                employeeChartData.value.push(tempEmployeeChartData.value)
            }


            //  购入
            if (chartData.value[Data].type === "购入") {
                // 购入词云
                tempInCloudChartData.value = {
                    name: chartData.value[Data].goodsName,
                    value: chartData.value[Data].dealCount,
                    dealTime: chartData.value[Data].dealTime
                }
                inCloudChartData.value.push(tempOutCloudChartData.value)


                //金额树形图
                tempMoneyTreeChartData.value = {
                    name: chartData.value[Data].goodsName,
                    outValue: 0,
                    inValue: chartData.value[Data].cost,
                    dealTime: chartData.value[Data].dealTime
                }
                moneyTreeChartData.value.push(tempMoneyTreeChartData.value)

                // 售出/购入量树形图
                tempCountTreeChartData.value = {
                    name: chartData.value[Data].goodsName,
                    outValue: 0,
                    inValue: chartData.value[Data].dealCount,
                    dealTime: chartData.value[Data].dealTime
                }
                countTreeChartData.value.push(tempCountTreeChartData.value)


                //员工评分雷达图
                tempEmployeeChartData.value = {
                    name: chartData.value[Data].userName,
                    count: chartData.value[Data].cost * 0.7,
                    quality: chartData.value[Data].goodsQuality,
                    dealCount: 1,
                    dealTime: chartData.value[Data].dealTime
                }
                employeeChartData.value.push(tempEmployeeChartData.value)
            }


            //划拨
            if (chartData.value[Data].type === "划拨") {
                //划拨词云
                tempTransferCloudChartData.value = {
                    name: chartData.value[Data].goodsName,
                    value: chartData.value[Data].dealCount,
                    dealTime: chartData.value[Data].dealTime
                }
                transferCloudChartData.value.push(tempTransferCloudChartData.value)
            }


            //饼图
            tempPanChartData.value = {
                name: chartData.value[Data].type,
                dealTime: chartData.value[Data].dealTime
            }
            panChartData.value.push(tempPanChartData.value)

            //地区热力图
            tempAreaHeatMapData.value = {
                type: chartData.value[Data].type,
                area: chartData.value[Data].wareHouseId,
                dealTime: chartData.value[Data].dealTime
            }
            areaHeatMapData.value.push(tempAreaHeatMapData.value)

            //员工业绩折线图
            tempEmployeePerformanceChartData.value = {
                name: chartData.value[Data].userName,
                value: chartData.value[Data].dealCount * 0.2 + chartData.value[Data].cost * 0.8,
                dealTime: chartData.value[Data].dealTime
            }
            employeePerformanceChartData.value.push(tempEmployeePerformanceChartData.value)

        }


        console.log("出售词云=============================")
        console.log(outCloudChartData)
        console.log("购入词云=============================")
        console.log(inCloudChartData)
        console.log("划拨词云=============================")
        console.log(transferCloudChartData)
        console.log("饼图=============================")
        console.log(panChartData)
        console.log("金额树形图=============================")
        console.log(moneyTreeChartData)
        console.log("售出/购入量树形图=============================")
        console.log(countTreeChartData)
        console.log("地区热力图=============================")
        console.log(areaHeatMapData)
        console.log("员工业绩折线图=============================")
        console.log(employeePerformanceChartData)
        console.log("员工评分雷达图=============================")
        console.log(employeeChartData)

    })
}

export {
    ChartData,
    CloudChartData,
    PanChartData,
    MoneyTreeChartData,
    CountTreeChartData,
    AreaHeatMapData,
    EmployeePerformanceChartData,
    EmployeeChartData,
    chartData,
    total,
    outCloudChartData,
    inCloudChartData,
    transferCloudChartData,
    panChartData,
    moneyTreeChartData,
    countTreeChartData,
    areaHeatMapData,
    employeePerformanceChartData,
    employeeChartData,
    getData
};